package web.action.com;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.joint.base.entity.Company;
import com.joint.base.entity.Duty;
import com.joint.base.entity.FileManage;
import com.joint.base.entity.system.Admin;
import com.joint.base.service.CompanyService;
import com.joint.base.service.UsersService;
import net.sf.json.JSONObject;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import web.action.BaseFlowAction;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ParentPackage("com")
public class AjaxAction extends BaseFlowAction {
	
	private static final long serialVersionUID = 2332217693929363302L;

    @Resource
    private UsersService usersService;
    @Resource
    private CompanyService companyService;

    private Admin admin;
    private Company org;

    private String flowName;

    private List<Duty> dutyList;
    private int number;
    private  List<ProcessDefinition> proDefinList;
    public String users(){
        return "users";
    }
    public String log(){return "log";}
    public String post(){
        return "post";
    }
    public String duty(){
        return "duty";
    }
    public String depart(){
        return "depart";
    }
    public String flow(){
        return "flow";
    }
    public String permission(){
        return "permission";
    }
    public String role(){
        return "role";
    }
    public String prostart(){
        return "prostart";
    }
    public String power(){
        return "power";
    }
    public String company(){
        org = usersService.getCompanyByUser();
        admin = adminService.get(org.getAdminId());
        return "company";
    }
    public String organization(){
        org = usersService.getCompanyByUser();
        number= companyService.findUsersByCompany(org).size();
        admin = adminService.get(org.getAdminId());
        //读取所有的部门
        return "organization";
    }
    public String dashboard(){
        return "dashboard";
    }

    public String modeler(){
        return "modeler";
    }
    public String systemSetting(){
        return ajaxHtmlCallback("400", "不提醒！","");
    }

    public List<Duty> getDutyList() {
        return dutyList;
    }

    public void setDutyList(List<Duty> dutyList) {
        this.dutyList = dutyList;
    }

    public  String prostudy(){
        return "prostudy";
    }
    public  String prolocate(){
        return "prolocate";
    }
    public  String export(){

        return "export";
    }

    /**
     * 上传文件
     * @return
     * @throws IOException
     */
    public String uploadFile() throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        FileManage fileManage;
        if(StringUtils.isNotEmpty(keyId)){
            fileManage = gridFSSave(new FileInputStream(filedata), filename, keyId);
        }else {
            fileManage = gridFSSave(new FileInputStream(filedata), filename);
        }
        data.put("fileId",fileManage.getId());
        data.put("state",200);
        data.put("name",fileManage.getName());
        data.put("url",fileManage.getUrl());
        data.put("msg","上传成功");
        return ajaxJson(JSONObject.fromObject(data).toString());
    }
    /**
     * 删除附件
     * @return
     */
    public String delFile(){
        if(StringUtils.isNotEmpty(keyId)){
            FileManage fileManage = fileManageService.get(keyId);
            fileManageService.delete(fileManage);
            return ajaxHtmlCallback("200", "删除成功！","操作状态");
        }else {
            return ajaxHtmlCallback("400", "请选择删除文件！","操作状态");
        }
    }
    public String fileList(){
        Map<String, Object> data = new HashMap<String, Object>();
        ArrayList<Map<String,Object>> dataRows=Lists.newArrayList();
        Map<String,Object> rMap ;
        if(StringUtils.isNotEmpty(keyId)){
            for(String fileId : StringUtils.split(keyId, ",")){
                FileManage fileManage = fileManageService.get(fileId);
                if(fileManage == null){
                    continue;
                }
                rMap =  Maps.newHashMap();
                rMap.put("fileId",fileManage.getId());
                rMap.put("name",fileManage.getName());
                rMap.put("url",fileManage.getUrl());
                dataRows.add(JSONObject.fromObject(rMap));
            }
        }
        data.put("dataRows",dataRows);
        data.put("num",dataRows.size());
        data.put("state",200);
        return ajaxJson(JSONObject.fromObject(data).toString());
    }
    public String getFlowName() {
        return flowName;
    }

    public void setFlowName(String flowName) {
        this.flowName = flowName;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public Company getOrg() {
        return org;
    }

    public void setOrg(Company org) {
        this.org = org;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

}
